package com.lsw.jgyf.ui.activity;

import android.graphics.Color;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.google.gson.Gson;
import com.lsw.jgyf.R;
import com.lsw.jgyf.model.JsonBean;
import com.lsw.jgyf.ui.base.activity.TitleBarActivity;
import com.lsw.jgyf.ui.fragment.HomeFragment1;
import com.lsw.jgyf.ui.fragment.HomeFragment2;
import com.lsw.jgyf.ui.fragment.RegisterDianyuanFragment;
import com.lsw.jgyf.ui.fragment.RegisterDianzhangFragment;
import com.lsw.util.JsonFileReader;
import com.lsw.util.LogUtils;

import org.json.JSONArray;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by lsw on 2018/7/8.
 */

public class RegisterActivity extends TitleBarActivity implements View.OnClickListener {
    @BindView(R.id.ll_dianyuan)
    LinearLayout ll_dianyuan;
    @BindView(R.id.cb_dianyuan)
    CheckBox cb_dianyuan;
    @BindView(R.id.tv_dianyuan)
    TextView tv_dianyuan;
    @BindView(R.id.ll_dianzhang)
    LinearLayout ll_dianzhang;
    @BindView(R.id.cb_dianzhang)
    CheckBox cb_dianzhang;
    @BindView(R.id.tv_dianzhang)
    TextView tv_dianzhang;

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    //首页
    private RegisterDianyuanFragment homeFragment;
    private RegisterDianzhangFragment homeFragment1;

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initEventAndData() {
        homeFragment = new RegisterDianyuanFragment();
        homeFragment1 = new RegisterDianzhangFragment();
        ll_dianyuan.setOnClickListener(this);
        ll_dianzhang.setOnClickListener(this);
        showFragment(0);
        cb_dianyuan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    cb_dianzhang.setChecked(false);
                    tv_dianzhang.setTextColor(mContext.getResources().getColor(R.color.color_666666));
                    tv_dianyuan.setTextColor(mContext.getResources().getColor(R.color.color_86B74E));
                    showFragment(0);
                }
            }
        });
        cb_dianzhang.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    cb_dianyuan.setChecked(false);
                    tv_dianyuan.setTextColor(mContext.getResources().getColor(R.color.color_666666));
                    tv_dianzhang.setTextColor(mContext.getResources().getColor(R.color.color_86B74E));
                    showFragment(1);
                }
            }
        });
    }

    @Override
    protected int setContentLayoutView() {
        return R.layout.activity_register;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_dianyuan:
                cb_dianyuan.setChecked(true);
                cb_dianzhang.setChecked(false);
                break;
            case R.id.ll_dianzhang:
                cb_dianyuan.setChecked(false);
                cb_dianzhang.setChecked(true);
                break;
        }
    }

    @Override
    protected void setActionBarDetail() {

    }


    public void showFragment(int position) {
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        switch (position) {
            case 0:
                if (!homeFragment.isAdded()) {
                    fragmentTransaction.add(R.id.fragment_register, homeFragment);
                }
                fragmentTransaction.show(homeFragment);
                fragmentTransaction.hide(homeFragment1);
                fragmentTransaction.commitAllowingStateLoss();

                break;
            case 1:
                if (!homeFragment1.isAdded()) {
                    fragmentTransaction.add(R.id.fragment_register, homeFragment1);
                }
                fragmentTransaction.hide(homeFragment);
                fragmentTransaction.show(homeFragment1);
                fragmentTransaction.commitAllowingStateLoss();
                break;
        }

    }



}
